package com.shenyi.mould.rest.controller.wx;

import com.shenyi.core.utils.Query;
import com.shenyi.wx.annotation.IgnoreAuth;
import com.shenyi.wx.annotation.LoginUser;
import com.shenyi.wx.api.IApiTopicService;
import com.shenyi.wx.entity.TopicDO;
import com.shenyi.wx.entity.UserDO;
import com.shenyi.wx.util.ApiBaseAction;
import com.shenyi.wx.util.ApiPageUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author richard.fan
 * @version 1.0
 * @date 2018/11/07
 * @desc
 * @see
 * @since 1.0
 */
@RestController
@RequestMapping("/api/topic")
public class ApiTopicController extends ApiBaseAction {

    @Autowired
    private IApiTopicService topicService;

    /**
     */
    @IgnoreAuth
    @GetMapping("list")
    public Object list(@RequestParam(value = "page", defaultValue = "1") Integer page,
                       @RequestParam(value = "size", defaultValue = "10") Integer size) {
        Map param = new HashMap();
        param.put("page", page);
        param.put("limit", size);
        param.put("sidx", "id");
        param.put("order", "desc");
        param.put("fields", "id, title, price_info, scene_pic_url,subtitle");
        //查询列表数据
        Query query = new Query(param);
        List<TopicDO> topicEntities = topicService.queryList(query);
        int total = topicService.queryTotal(query);
        ApiPageUtils pageUtil = new ApiPageUtils(topicEntities, total, query.getLimit(), query.getPage());
        return toResponsSuccess(pageUtil);
    }

    /**
     */
    @IgnoreAuth
    @GetMapping("detail")
    public Object detail(@LoginUser UserDO loginUser, Integer id) {
        TopicDO topicEntity = topicService.queryObject(id);
        return toResponsSuccess(topicEntity);
    }

    /**
     */
    @IgnoreAuth
    @GetMapping("related")
    public Object related(@LoginUser UserDO loginUser, Integer id) {
        Map param = new HashMap();
        param.put("limit", 4);
        List<TopicDO> topicEntities = topicService.queryList(param);
        return toResponsSuccess(topicEntities);
    }
}
